package com.github.wings27.interview.Problem;

/**
 * Project Name: common-interview-problems
 * Date Created: 2014/02/18 17:57
 *
 * @author Wings
 */
public class Fibonacci {
    public static void main(String[] args) {
        final int UPPER = 2;
        for (int i = 0; i < UPPER; i++) {
            System.out.println(String.valueOf(i) + ":" + fib_recursive(i));
        }
        System.out.println("============");
        for (int i = 0; i < UPPER; i++) {
            System.out.println(String.valueOf(i) + ":" + fib(i));
        }
    }

    public static int fib(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return 1;
        } else {
            int a = 1;
            int b = 1;
            while (--n > 0) {
                int temp = a + b;
                a = b;
                b = temp;
            }
            return b;
        }
    }

    public static int fib_recursive(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return 1;
        } else {
            return fib_recursive(n - 1) + fib_recursive(n - 2);
        }

    }
}
